package com.gxuwz.exception;

import com.gxuwz.result.Code;
import com.gxuwz.result.ResultInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.ModelAndView;

/**
 * 获取整个程序的异常
 */
@RestControllerAdvice
@Slf4j
public class ProjectExceptionAdvice {

    @ExceptionHandler(SystemException.class)
    public ResultInfo doSystemException(SystemException ex){
        //记录日志
        log.info("系统错误");
        return new ResultInfo(null,ex.getCode(), ex.getMessage());
    }

    @ExceptionHandler(BusinessException.class)
    public ResultInfo doBusinessException(BusinessException ex){
        log.info("业务错误");
        return new ResultInfo(null, ex.getCode(),ex.getMessage());
    }

    @ExceptionHandler(Exception.class)
    public ModelAndView doException(Exception ex){
        log.info("系统未知错误");
        ex.printStackTrace();
        ModelAndView modelAndView = new ModelAndView();
        ResultInfo resultInfo = new ResultInfo(null, Code.SYSTEM_UNKNOW_ERR, "系统繁忙，请稍后再试！！！");
        modelAndView.addObject("resultInfo",resultInfo);
        modelAndView.setViewName("500");
        return modelAndView;
    }
}
